package com.lilei.serialize;

import com.thoughtworks.xstream.XStream;

public class XtreamSerializer implements ISerializer {
    private static final XStream xStream = new XStream();
    /*
        需要引入jar包
        <!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.11.1</version>
        </dependency>

        不能序列化translate修饰的字段
     */

    @Override
    public <T> byte[] serialize(T obj) {
        return xStream.toXML(obj).getBytes();
    }

    @Override
    public <T> T deserialize(byte[] data, Class<T> clazz) {
        return (T) xStream.fromXML(new String(data));
    }
}
